<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">






      <div class="game-selector">
        <div v-for="(item, index) in gmList" :key="index" :class="{ selected: item.id === selectedGameId }"
          @click="selectGame(item.id)">
          <span>{{ item.gameName }}</span>
          <br v-if="index % 4 === 2 || index === gmList.length - 1">
        </div>
      </div>


    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:inventory:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:inventory:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:inventory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:inventory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号信息表" align="center" prop="id" />
      <el-table-column label="游戏" align="center" prop="gmId" class-name="custom-cell">
        <template slot-scope="scope">
          <span v-for="(item, index) in gmList" :key="index">
            <span v-if="item.id == scope.row.gmId">
              {{ item.gameName || '未知游戏' }}
            </span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="首图" align="center" prop="firstPicture">
        <template slot-scope="scope">
          <div class="full-size-image rounded-square">
            <el-image :src="scope.row.firstPicture" style="width: 100%; height: auto;">
              <div slot="placeholder" class="image-slot">
                <span class="dot">...</span>
              </div>
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="游戏账号" align="center" prop="gameAccount" />
      <el-table-column label="商品标题" align="center" prop="productTitle" />
      <el-table-column label="商品信息" align="center" prop="productInformation" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="账号类型" align="center" prop="typeAcc" />
      <el-table-column label="区服" align="center" prop="region" />
      <el-table-column label="唯一编号" align="center" prop="gameId" />
      <el-table-column label="销售价" align="center" prop="salesPrice" />
      <el-table-column label="是否推荐" align="center" prop="isRecommend" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isRecommend" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
            inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="前端是否显示" align="center" prop="isItDisplayed" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isItDisplayed" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
            inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="logicalDeletion" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.logicalDeletion" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
            inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inventory:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- 第一行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="游戏" prop="gmId">
              <el-select v-model="form.gmId" placeholder="请选择游戏">
                <el-option v-for="(item, index) in gmList" :key="index" :label="item.gameName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="游戏账号" prop="gameAccount">
              <el-input v-model="form.gameAccount" placeholder="请输入游戏账号" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第五行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="销售价" prop="salesPrice">
              <el-input v-model="form.salesPrice" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第六行 -->
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号类型" prop="typeAcc">
              <el-input v-model="form.typeAcc" placeholder="请输入账号类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区服" prop="region">
              <el-input v-model="form.region" placeholder="区服" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="商品标题" prop="productTitle">
              <el-input v-model="form.productTitle" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- 第四行 -->
            <el-form-item label="商品信息" prop="productInformation">
              <el-input v-model="form.productInformation" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

        </el-row>
        <!-- 第二行 -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="首图" prop="firstPictures">
              <el-row>
                <el-col>
                  <imageUploadMore ref="pic_single" :isShowTip="false" :limit="1" :value="form.firstPictures" />
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行 -->
        <el-row>
          <el-col :span="24">
            <el-form-item label="详情图" prop="detailedDiagram">
              <el-row>
                <el-col>
                  <imageUploadMore ref="pic_more" :isShowTip="false" :limit="10" :value="form.detailedDiagrams" />
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listInventory, getInventory, delInventory, addInventory, updateInventory, category, updaType } from "@/api/gmInventory/inventory";
import imageUploadMore from '@/components/ImageUpload/imageUploadMore.vue'
export default {
  name: "Inventory",
  components: {
    imageUploadMore
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 类型
      type: {},
      // 表格数据
      inventoryList: [],
      // 游戏信息
      gmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gmId: null,
        isItDisplayed: null,
        logicalDeletion: null,
        firstPicture: null,
        detailedDiagram: null,
        gameAccount: null,
        productTitle: null,
        productInformation: null,
        sort: null,
        firstPictures: [],
        detailedDiagrams: []
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        gmId: [
          { required: true, message: "游戏表id不能为空", trigger: "blur" }
        ],
        firstPicture: [
          { required: true, message: "首图不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getgm();
  },
  methods: {


    handleStatusChange(row) {
      this.type = {
        isItDisplayed: row.isItDisplayed,
        logicalDeletion: row.logicalDeletion,
        isRecommend: row.isRecommend,
        id: row.id
      }
      updaType(this.type).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false
        this.getList();
      })
    },

    /** 提交按钮 */
    submitForm() {
      // 单张图片
      let firstPictures = this.$refs.pic_single.getImgArrByChild();
      let detailedDiagrams = this.$refs.pic_more.getImgArrByChild();
      this.form.firstPictures = firstPictures;
      this.form.detailedDiagrams = detailedDiagrams;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInventory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInventory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    getgm() {
      category().then(ref => {
        this.gmList = ref.data
      })
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      listInventory(this.queryParams).then(response => {
        this.inventoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gmId: null,
        isItDisplayed: null,
        logicalDeletion: null,
        firstPicture: null,
        detailedDiagram: null,
        gameAccount: null,
        productTitle: null,
        productInformation: null,
        sort: null,
        firstPictures: [],
        detailedDiagrams: []

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */



    selectGame(id) {
      this.selectedGameId = id;
      this.fetchGameData(id); // 使用新的函数处理游戏选择
    },
    fetchGameData(id) {
      // 在这里替换为你的实际逻辑，例如获取游戏详情、更新查询参数等
      this.queryParams.gmId = id;
      this.queryParams.pageNum = 1;
      this.getList();
    },




    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInventory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return delInventory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inventory/export', {
        ...this.queryParams
      }, `inventory_${new Date().getTime()}.xlsx`)
    }
  },

  /** 删除按钮操作 */
  handleDelete(row) {
    const ids = row.id || this.ids;
    this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
      return delCategory(ids);
    }).then(() => {
      this.getList();
      this.$modal.msgSuccess("删除成功");
    }).catch(() => { });
  },
};
</script>
<style lang="scss" scoped>
.custom-tabs .el-tabs__nav-wrap {
  width: 100%;
  height: 200px;
}

.custom-tabs .el-tabs__nav {
  height: 50px;
  /* 调整高度 */
  line-height: 50px;
  /* 调整行高 */
}

.custom-tabs .el-tabs__item {
  font-size: 18px;
  /* 调整字体大小 */
  padding: 0 20px;
  /* 调整内边距 */
}


.custom-cell {
  background-color: #F5F7FA;
  /* 更改为你喜欢的颜色 */
  padding: 8px;
  /* 添加内边距 */
  border-radius: 4px;
  /* 添加圆角 */
  font-weight: bold;
  /* 可选：使文本更突出 */
  color: #333;
  /* 可选：更改文本颜色 */
  vertical-align: middle;
  /* 确保内容居中对齐 */
}
.game-selector {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.game-selector div {
  cursor: pointer;
  position: relative;
  display: inline-block;
  padding: 8px 15px;
  background-color: #f5f5f5;
  color: #333;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s ease;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
  }

  &:hover {
    background-color: #e9e9e9;
    color: #212121;
  }

  &.selected {
    background-color: #4fc3f7;
    color: #fff;
    border-color: #4fc3f7;

    &::before {
      background-color: #4fc3f7;
    }
  }
}


</style>
